package com.xwaf.platform.web.config;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class LocaleMessage {

	@Autowired
	private MessageSource messageSource;

	/**
	 * 
	 * <p>
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2019年12月30日 上午9:22:57 <br>
	 * @see 无<br>
	 * @param code
	 *            对应文本配置的key
	 * @return 对应地区的语言消息字符串
	 * @demo <br>
	 */
	public String getMessage(String code) {
		return this.getMessage(code, new Object[] {});
	}

	public String getMessage(String code, String defaultMessage) {
		return this.getMessage(code, null, defaultMessage);
	}

	public String getMessage(String code, String defaultMessage, Locale locale) {
		return this.getMessage(code, null, defaultMessage, locale);
	}

	public String getMessage(String code, Locale locale) {
		return this.getMessage(code, null, "", locale);
	}

	public String getMessage(String code, Object[] args) {
		return this.getMessage(code, args, "");
	}

	public String getMessage(String code, Object[] args, Locale locale) {
		return this.getMessage(code, args, "", locale);
	}

	public String getMessage(String code, Object[] args, String defaultMessage) {
		Locale locale = LocaleContextHolder.getLocale();
		return this.getMessage(code, args, defaultMessage, locale);
	}

	public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
		return messageSource.getMessage(code, args, defaultMessage, locale);
	}

	public Locale getLocale() {
		return LocaleContextHolder.getLocale();
	}

	/**
	 * 
	 * <p>
	 * 获取I18N解析后的返回信息
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2020年1月3日 下午10:14:31 <br>
	 * @see 无<br>
	 * @param result
	 * @return
	 * @demo <br>
	 */
	public String getI18nResult(Object result) {
		ObjectMapper mapper = new ObjectMapper();
		String resultVal;
		try {
			resultVal = mapper.writeValueAsString(result);
			Pattern pattern = Pattern.compile("\"(i18n\\..+?)\"");
			Matcher matcher = pattern.matcher(resultVal);
			StringBuffer i18nBuf = new StringBuffer();
			while (matcher.find()) {
				String i18nText = matcher.group();
				i18nText = i18nText.substring(1, i18nText.length() - 1);
				String i18nValue = "\"" + this.getMessage(i18nText, i18nText) + "\"";
				matcher.appendReplacement(i18nBuf, i18nValue);
			}
			matcher.appendTail(i18nBuf);
			return i18nBuf.toString();
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result.toString();
	}
}
